package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import javax.persistence.*;

import play.data.validation.Email;
import play.data.validation.Password;
import play.data.validation.Required;
import play.db.jpa.Model;

@Table(name="import$Agent")
@Entity
public class Agent extends Model{
	
	@Column(unique=true)
	public String nomUtilisateur;
	
	@OneToMany
	public List<Participation> participationsFMPA;
	
	@OneToMany
	public List<Stage> organisateurStage;
	
	@OneToMany
	public List<Stage> gestionnaireStage;
	
	@Required
	public String nom;

	@ManyToOne
	public CIS cis;

	@Required
	public String prenom;

	public Date dateNais;

	@Required
	@ManyToOne
	public Grade grade;

	@Required
	@ManyToOne
	public Statut statut;

	@Email
	public String email;

	@Password
	public String password;

	@ManyToOne
	public NivFor niv;
	
	@ManyToMany
	public List<Fonction> fonction;
	
	@OneToMany(cascade=CascadeType.ALL)
	public List<Candidat> candidatures;
	
	public Agent(String nom, String prenom, Date dateNais,CIS cis,
			Grade grade,Statut statut,String pass,String mail){
		this.nom = nom;
		this.prenom = prenom;
		this.dateNais = dateNais;
		this.cis=cis;
		this.grade=grade;
		this.statut=statut;
		this.password=pass;
		this.email=mail;
		candidatures=new ArrayList<Candidat>();
		fonction=new ArrayList<Fonction>();
	}

	public String fullname(){
		return nom+" "+prenom;
	}

	public String toString(){
		return nom+" "+prenom;
	}

	public void addCandidature(Candidat candidat){
		candidatures.add(candidat);
	}

	public void setCandidatures(List<Candidat> candidatures) {
		this.candidatures = candidatures;
	}

	public List<Candidat> getCandidatures() {
		return candidatures;
	}

	public boolean equals(Agent a){
		return a.nomUtilisateur.equals(this.nomUtilisateur);
	}

	public CIS getCisService() {
		return cis;
	}

	public void setCisService(CIS cisService) {
		this.cis = cisService;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<Agent> getAgents(Long id_cis){
		CIS cis = CIS.findById(id_cis);
		List<Agent> agents = Agent.find("cisService like ?", cis).fetch();
		return agents;
	}
}
